----
-- filename : gamescreen.lua
-- description : gamescreen.lua is script for logic in game screen
----

-- Load needed library/script
local storyboard = require "storyboard"
local player = require "player"
local board = require "board"
local layer = require "layer"
local pion = require "pion"
local scene = storyboard.newScene()

-- Local variable
local player0,player1,board1

function scene:createScene(event)
	print( "Create Game Scene" )

	-- generate layer number
	layer.generateLayerGroup(self.view,14)

	local xboard = (479-43*7)/2;
	local yboard = (340-30-43*7)+7;

	-- background image
	local back = display.newImageRect( "assets/bg.png",480, 360, true)
	back.x = 240
	back.y = 160
	layer.insertImage(layer.BACKGROUND, back)

	-- create 2 new players
	player0 = player.new() -- default character
	player1 = player.new("rick") -- 'rick' character

	-- load board from json file
	board1 = board.new(xboard,yboard,43,43,"levels/dummy.json")
	
	-- generate dices for player 0
	player0:createPion(board1,2,1)
	player0:createPion(board1,3,1)
	player0:createPion(board1,4,1)
	player0:createPion(board1,5,1)
	player0:createPion(board1,6,1)
	player0:startTurn() -- start first

	-- generate dices for player 1
	player1:createPion(board1,2,7)
	player1:createPion(board1,3,7)
	player1:createPion(board1,4,7)
	player1:createPion(board1,5,7)
	player1:createPion(board1,6,7)
	player1:endTurn()
	
	-- load HUD
	local hud_base_title = display.newImageRect( "assets/hud_base_title.png",197, 27 , true )
	hud_base_title.x = (480)/2;
	hud_base_title.y = 18+display.screenOriginY-3;

	local hud_versus_title = display.newImageRect( "assets/hud_versus_title.png",85, 23, true )
	hud_versus_title.x = (480)/2;
	hud_versus_title.y = 18+display.screenOriginY-3;

	-- add HUD to layer HUD
	layer.insertImage(layer.HUD, hud_base_title);
	layer.insertImage(layer.HUD, hud_versus_title);

end

-- touch logic
function scene:touch( event ) 
	if (event.phase == "began") then
		if(pion.cPion ~= nil) then
			pion.cPion:setFloat(false)
		end
		layer.clean(layer.DIR)
	end
	return true
end

-- add event listener
Runtime:addEventListener("touch", scene)
scene:addEventListener("createScene", scene)

return scene